package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.dto.UserByMyPersonalDetailDto;
import com.woniu.model.dto.UserPersonalClassMarginDto;
import com.woniu.model.form.AddLeagueClassDetailForm;
import com.woniu.model.form.AddPersonalClassDetailForm;
import com.woniu.model.param.AddPersonalClassDetailParam;
import com.woniu.service.YPersonalDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 胡歌
 * @since 2024年05月28日
 */
@RestController
@RequestMapping("/yPersonalDetail")
@Api(tags = "私教课详情跟踪接口")
public class YPersonalDetailController {
    @Resource
    private YPersonalDetailService personalDetailService;
    @Resource
    private RedisUtil redisUtil;


    /**
     * 通过用户token查该用户的私课细节（上课时间）
     * @param token
     * @return
     */
    @GetMapping("/userByMyPersonalDetail")
    @ApiOperation("通过用户token查该用户的私课细节（上课时间）")
    public  Result<UserByMyPersonalDetailDto> userByMyPersonalDetail( @RequestHeader String token) {

        //拿id
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("userId").toString());

        UserByMyPersonalDetailDto dto = personalDetailService.userByMyPersonalDetail(userId);

        return Result.newInstance(UserByMyPersonalDetailDto.class).setData(dto).setMsg("查询成功").setCode(200);
    }

    /**
     * 教练新增新增私教详细跟踪
     * @param form
     * @return
     * @throws YogaException
     */
    @PostMapping("/addPersonalClassDetail")
    @ApiOperation(value = "教练新增私教详细跟踪")
    public Result addPersonalClassDetail(@RequestBody AddPersonalClassDetailForm form, @RequestHeader String token) throws YogaException {
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());

        AddPersonalClassDetailParam param = BeanUtil.toBean(form, AddPersonalClassDetailParam.class);
        param.setCoachId(coachId);

        personalDetailService.addPersonalClassDetail(param);
        return Result.getResult().setMsg("新增成功");
    }

    /**
     * 教练私教课完成
     * @param id 私教详情的id
     * @return
     * @throws YogaException
     */
    @GetMapping("/finishPersonalClassDetail")
    @ApiOperation(value = "教练私教课完成")
    public Result finishPersonalClassDetail(Integer id, @RequestHeader String token) throws YogaException {
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());


        personalDetailService.finishPersonalClassDetail(id,coachId);
        return Result.getResult().setMsg("完成成功");
    }

    /**
     * 教练私教课取消
     * @param id 私教详情的id
     * @return
     * @throws YogaException
     */
    @GetMapping("/cancelPersonalClassDetail")
    @ApiOperation(value = "教练私教课取消")
    public Result cancelPersonalClassDetail(Integer id) throws YogaException {
        personalDetailService.cancelPersonalClassDetail(id);
        return Result.getResult().setMsg("取消成功");
    }

    /**
     * 根据教练id查询已预约的私教课
     * @param token
     * @return
     */
    @GetMapping("/loadPersonalClassByCoachId")
    @ApiOperation("根据教练id查询已预约的私教课")
    public Result<List<UserPersonalClassMarginDto>> loadPersonalClassByCoachId(@RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());

        List<UserPersonalClassMarginDto> personalStudents = personalDetailService.loadPersonalClassByCoachId(coachId);
        return Result.newListInstance(UserPersonalClassMarginDto.class).setData(personalStudents);
    }
}

